package com.woniuxy.core.mapper;

import com.woniuxy.entity.AllRun;
import com.woniuxy.entity.AllUser;
import com.woniuxy.entity.DTO.RunPostMessageDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

public interface RunMapper {
    List<RunPostMessageDTO> getAll(@Param("state") Integer state,
                                   @Param("userId") Integer userId,
                                   @Param("takeUserId") Integer takeUserId);

    @Select("select * from all_run where id = #{id}")
    AllRun getRunById(Integer id);

    @Select("select * from all_user where user_id = #{userId}")
    AllUser getUserById(Integer userId);

    @Update("update all_run set take_user_id = #{takeUserId},update_time = NOW()," +
            "state = 1 where run_number = #{runNumber}")
    void updateRun(@Param("takeUserId") Integer takeUserId,@Param("runNumber") String runNumber);

    @Insert("insert into all_run(user_id,title,run_describe,price,startaddress," +
            "endaddress,type_id,create_time,update_time,deadline,run_number,code," +
            "phone_end,remark)" +
            "values (#{allRun.userId},#{allRun.title},#{allRun.runDescribe}," +
            "#{allRun.price},#{allRun.startaddress},#{allRun.endaddress}," +
            "#{allRun.typeId},#{allRun.createTime},NOW(),#{allRun.deadline}," +
            "#{allRun.runNumber},#{allRun.code},#{allRun.phoneEnd},#{allRun.remark})")
    void addRun(@Param("allRun") AllRun allRun);
    @Select("select * from all_run where run_number = #{runNumber}")
    AllRun getRunByRN(String runNumber);
    @Select("select * from all_run where state != 1 and deadline <= #{now}")
    List<AllRun> selectExpiredRuns(LocalDateTime now);
    @Update("update all_run set state = 5 where id = #{id}")
    void updateRunState(Integer id);
    @Update("update all_run set state = 2 where id = #{id}")
    void updateState(Integer id);
}
